package com.yuhanginfo.modules.web.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebInvoice;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebInvoiceService;

/**
 * 开票管理Controller
 * @author lvls
 * @version 2017-10-30
 */
@Controller
@RequestMapping(value = "${frontPath}/webInvoice")
public class WebInvoiceController extends BaseController {

	@Autowired
	private WebInvoiceService WebInvoiceService;

	@RequiresPermissions("user")
	@RequestMapping(value = {"list", ""})
	public String list(WebInvoice invoice, Model model) {
		model.addAttribute("actionCenter", "webInvoice");
		invoice.setUserId(WebUserUtils.getYongUser().getId());
		List<WebInvoice> list = WebInvoiceService.findList(invoice); 
		model.addAttribute("list", list);
		return "modules/web/webInvoiceList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	public String save(WebInvoice invoice, Model model) {
		WebInvoiceService.saveWebInvoice(invoice);
		model.addAttribute("resultMessage", "保存成功");
		return this.list(new WebInvoice(),model);
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WebInvoice invoice, Model model) {
		WebInvoiceService.delete(invoice);
		return "1";
	}
	
	@RequestMapping(value = "getDefInvoice")
	@ResponseBody
	public WebInvoice getDefInvoice() {
		WebYongUser yong = WebUserUtils.getYongUser();
		String userId = yong.getId();
		return WebInvoiceService.getDefInvoice(userId);
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = "setDef")
	@ResponseBody
	public String setDef(WebInvoice invoice) {
		try{
		WebInvoiceService.setDef(invoice);
		}catch(Exception ex){
			logger.error(ex.getMessage(),ex);
			return "false";
		}
		return "true";
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = "selectByUserId")
	@ResponseBody
	public List<WebInvoice> selectByUserId(WebInvoice invoice, Model model) {
		invoice.setUserId(WebUserUtils.getYongUser().getId());
		return WebInvoiceService.findList(invoice); 
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = "saveReturn")
	@ResponseBody
	public WebInvoice saveReturn(WebInvoice invoice, Model model) {
		WebInvoiceService.saveWebInvoice(invoice);
		return invoice;
	}

}